﻿#pragma once

#include <boost/asio.hpp>
#include "libdaq/backend/base_backend.h"

namespace libdaq {
namespace backend {
class LockzhinerSocketBackend : public LockzhinerBaseBackend {
public:
  LockzhinerSocketBackend();
  ~LockzhinerSocketBackend() override;
  LockzhinerBackendType GetBackendType() override { return LockzhinerBackendType::SocketBackend; }
  bool Initialize() override;
  bool Open() override {
    is_opened_ = true;
    return socket_.is_open();
  }
  bool Close() override; // 关闭连接

  static void SigintHandler(int signal);

  bool ReadData(unsigned char *data, int read_size, int *actual_size, int duration_ms) override;
  bool WriteData(unsigned char *data, int write_size, int *actual_size, int duration_ms) override;
  boost::asio::ip::tcp::tcp::socket &GetSocket() { return socket_; }
  std::string GetIPAddress() const { return socket_.remote_endpoint().address().to_string(); }
  void SetReceiveOption(); // 设置接收选项
  
public:
  static LockzhinerSocketBackend *global_socket_backend_; 

private:
  boost::asio::io_context io_context_;
  boost::asio::ip::tcp::tcp::socket socket_;
};
}
}
